package com.etheart.lyric.htmlparser.action;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.htmlparser.util.ParserException;
import org.springframework.beans.factory.annotation.Autowired;

import com.etheart.framework.core.dao.CrudService;
import com.etheart.lyric.htmlparser.action.parse.DownLyricThread;
import com.etheart.lyric.htmlparser.action.parse.JujingParseThread;
import com.etheart.lyric.htmlparser.action.parse.StaticState;
import com.etheart.music.service.AlbumService;
import com.etheart.music.service.SingerService;
import com.etheart.music.service.SongService;
import com.opensymphony.xwork2.ActionSupport;

  
public class JujingParseAction extends ActionSupport implements ApplicationAware,RequestAware{
	private static final long serialVersionUID = 1L;
	
	@Autowired             
	private SingerService singerService;               
	@Autowired                
	private AlbumService albumService;        
	@Autowired     
	private SongService songService;       
	@Autowired   
	private CrudService crudService;
	
	
	DownLyricThread downLyricThread;

	private String rootDir;
	private String songNetId=null;
	private String singerNetId;
	private String singerFileNames;
	private String albumNetId;  
	private int singerId;       
	private String singerLanguage;
	private String singerSex;
	private boolean isTeam;
	private long matchAlbumNetId=1l;
	/**
	 * @return
	 * @throws ParserException
	 * @throws IOException
	 */
	
	@Action(value="parse_refresh",
            results={@Result(name="success", location="/jujing/refresh.jsp")})  
	public String refresh() {
		request.put("cur_singer", StaticState.cur_singer);
		request.put("cur_album", StaticState.cur_albume);
		request.put("handleSingers", StaticState.handleSingers);
		request.put("handleAlbums", StaticState.handleAlbums);
		return "success";
	}
	@Action(value="parse_restart",
            results={@Result(name="success", location="/jujing/refresh.jsp")})  
	public String restart() {
		JujingParseThread parseThread = getInitedParseThread();
		if (parseThread!=null&&!parseThread.isAlive()) {
			parseThread.albumNetId=albumNetId;
			parseThread.singerNetId=singerNetId;
			parseThread.songNetId=songNetId;
			parseThread.start();
		}
		return "success";
	}
	
	private Long startSongId,endSongId;
	private String saveDir = "lyric";
	@Action(value="down_lyric",
            results={@Result(name="success", location="/jujing/refresh.jsp")})  
	public String downlyric() {
		downLyricThread = getInitedLyricDownThread();
		downLyricThread.setMirrorPath(rootDir);
		downLyricThread.setSavePath(saveDir);
		downLyricThread.setStartSongId(startSongId);
		downLyricThread.setEndSongId(endSongId);
		if (downLyricThread!=null&&!downLyricThread.isAlive()) {
			downLyricThread.start();
		} 
		return "success";
	}
	
	private JujingParseThread getInitedParseThread() {
		Object thread = application.get("parseThread");
		if (thread != null) {
			JujingParseThread parseThread=(JujingParseThread) thread; 
			if (parseThread.isStart && parseThread.isAlive()) {
				return null;
			}
		} 
		if (StringUtils.isBlank(rootDir)) {
			return null;
		}
		File dir = new File("D:\\qqmusic\\巨鲸\\mirror\\www.top100.cn\\artist");
		JujingParseThread parseThread = new JujingParseThread(singerService,albumService,songService, dir,rootDir);
		application.put("parseThread", parseThread);
		return parseThread;
	}
	
	private DownLyricThread getInitedLyricDownThread() {
		Object thread = application.get("downLyricThread");
		if (thread!= null) {
			downLyricThread=(DownLyricThread) thread; 
			if ( downLyricThread.isAlive()) {
				return null;
			}
		} 
		if (StringUtils.isBlank(rootDir)) {
			return null;
		}
		
		downLyricThread = new DownLyricThread(startSongId, endSongId, songService, singerService, albumService, crudService, rootDir, "lyric");
		application.put("downLyricThread", downLyricThread);
		return downLyricThread;
	}
	
	
	

	public String getRootDir() {
		return rootDir;
	}

	public void setRootDir(String rootDir) {
		this.rootDir = rootDir;
	}
	
	

	public String getSongNetId() {
		return songNetId;
	}
	public void setSongNetId(String songNetId) {
		this.songNetId = songNetId;
	}
	public String getSingerNetId() {
		return singerNetId;
	}
	public void setSingerNetId(String singerNetId) {
		this.singerNetId = singerNetId;
	}
	public String getAlbumNetId() {
		return albumNetId;
	}
	public void setAlbumNetId(String albumNetId) {
		this.albumNetId = albumNetId;
	}



	Map<String, Object> application;
	Map<String, Object> request;
	public void setApplication(Map<String, Object> arg0) {
		this.application = arg0;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request= request;
	}

	public int getSingerId() {
		return singerId;
	}

	public void setSingerId(int singerId) {
		this.singerId = singerId;
	}

	public String getSingerLanguage() {
		return singerLanguage;
	}

	public void setSingerLanguage(String singerLanguage) {
		this.singerLanguage = singerLanguage;
	}

	public String getSingerSex() {
		return singerSex;
	}

	public void setSingerSex(String singerSex) {
		this.singerSex = singerSex;
	}



	public boolean isTeam() {
		return isTeam;
	}



	public void setTeam(boolean isTeam) {
		this.isTeam = isTeam;
	}



	public String getSingerFileNames() {
		return singerFileNames;
	}



	public void setSingerFileNames(String singerFileNames) {
		this.singerFileNames = singerFileNames;
	}


	public long getMatchAlbumNetId() {
		return matchAlbumNetId;
	}


	public void setMatchAlbumNetId(long matchAlbumNetId) {
		this.matchAlbumNetId = matchAlbumNetId;
	}
	public Long getStartSongId() {
		return startSongId;
	}
	public void setStartSongId(Long startSongId) {
		this.startSongId = startSongId;
	}
	public Long getEndSongId() {
		return endSongId;
	}
	public void setEndSongId(Long endSongId) {
		this.endSongId = endSongId;
	}
	public String getSaveDir() {
		return saveDir;
	}
	public void setSaveDir(String saveDir) {
		this.saveDir = saveDir;
	}
	public void setSingerService(SingerService singerService) {
		this.singerService = singerService;
	}
	public void setAlbumService(AlbumService albumService) {
		this.albumService = albumService;
	}
	public void setSongService(SongService songService) {
		this.songService = songService;
	}
	public void setCrudService(CrudService crudService) {
		this.crudService = crudService;
	}

	
	
}
